// pages/plans/plans.js
var app =getApp();
var openid = '';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isBind1:true,
        isBind2:false,
        showpage:1
    },
    changeClass1:function(){
        this.setData({
            isBind2:false,
            isBind1:true,
            showpage:1
        })
    },
    changeClass2:function(){
        this.setData({
            isBind2:true,
            isBind1:false,
            showpage:2
        })
        var that = this;
        // 获取数据
        wx.request({
          url: 'http://127.0.0.1:8000/userPlandone?openid='+openid,
          success:function(res){
            console.log(res)
            that.setData({
              donedata:res.data.data
            })
          }
        })
    },
    // 打卡,当日次数累加并弹出(判断是否加满),总天数累加(doneSec==Sec时自动加并弹窗提示)
    submitcard:function(res){
      var that = this;
      var index = res.currentTarget.dataset.index;
      var id = this.data.plandata[index].id;
      var sec = this.data.plandata[index].Seconds;
      var donesec = this.data.plandata[index].doneSec;
      var doneday = this.data.plandata[index].doneDays;
      var day = this.data.plandata[index].Alldays;
      if(donesec === sec){
        wx.showToast({
          title: '今天完成任务啦😄',
          icon:'none'
        })
      }
      else if (sec-donesec>1){
        donesec+=1;
        wx.request({
          url: 'http://127.0.0.1:8000/submitPlanSec?id='+id,
          success:function(res){
            console.log(res)
            var s = 'plandata['+index+'].doneSec'
            that.setData({
              [s]:res.data
            })
          }
        })
      }
      else if (sec-donesec === 1){
        // 增加次数和天数
        donesec+=1;
        doneday+=1;
        wx.request({
          url: 'http://127.0.0.1:8000/submitPlanDay?id='+id,
          success:function(res){
            console.log(res)
            var s = 'plandata['+index+'].doneSec'
            var d = 'plandata['+index+'].doneDays'
            that.setData({
              [s]:res.data.donesec,
              [d]:res.data.doneday
            })
          }
        })
      }
      // 提交到django
    },
    toaddPlans:function(){
        wx.navigateTo({
          url: '../addplan/addplan',
        })

    },
    // 完成今日打卡函数，向django发送数据
    doneClock:function(){
        
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that =this;
        // 登录
        wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              wx.request({
                url: 'http://127.0.0.1:8000/Login?code='+res.code,
                success:function(res){
                  console.log(res)
                  var code = res.data.code;
                  openid = res.data.openid;
                  app.globalData.openid = openid;
                  if(code===404){
                    wx.navigateTo({
                      url: '/pages/login/login?openid='+openid,
                    })
                  }
                  wx.request({
                    url: 'http://127.0.0.1:8000/userPlaning?openid='+openid,
                    success:function(res){
                      console.log(res)
                      that.setData({
                          plandata:res.data.data
                      })
                    }
                  })
                }
              })
              
            }
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.onLoad();

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})